package com.haohope.framework.ureport2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
public class Ureport2ServiceImpl extends  AbstractUreport2Service {
	private final JdbcTemplate jdbcTemplate;
	public Ureport2ServiceImpl(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public class Ureport2FileRowMapper implements RowMapper<Ureport2File>{
		@Override
		public Ureport2File mapRow(ResultSet rs, int rowNum) throws SQLException {
			Ureport2File ureport2File = new Ureport2File();
			ureport2File.setId(rs.getLong("id"));
			ureport2File.setCreatetime(rs.getDate("createtime"));
			ureport2File.setDeleteflag(rs.getBoolean("deleteflag"));
			ureport2File.setFilecontent(rs.getString("filecontent"));
			ureport2File.setFilename(rs.getString("filename") );
			ureport2File.setFiletitle(rs.getString("filetitle"));
			ureport2File.setPrefix(rs.getString("prefix") );
			ureport2File.setUpdatetime(rs.getDate("updatetime") );
            return ureport2File;
		}
    }
	private void savefile(Ureport2File file) {
		//jdbcTemplate.update("insert into ureport2_file(prefix,filename,filecontent,deleteflag,createtime,updatetime) values(?,?,?,?,?,?) ",   
        //        new Object[]{file.getPrefix(),file.getFilename(),file.getFilecontent(),file.getDeleteflag(),file.getCreatetime(),file.getUpdatetime()});
		KeyHolder holder = new GeneratedKeyHolder();
		jdbcTemplate.update(new PreparedStatementCreator() {
			@Override
			public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
				PreparedStatement ps = connection.prepareStatement("insert into wb_ureports(prefix,filename,filecontent,deleteflag,createtime,updatetime) values(?,?,?,?,?,?) ",Statement.RETURN_GENERATED_KEYS);
				ps.setObject(1, file.getPrefix());
				ps.setObject(2, file.getFilename());
				ps.setObject(3, file.getFilecontent());
				ps.setObject(4,file.getDeleteflag());
				ps.setObject(5, file.getCreatetime());
				ps.setObject(6, file.getUpdatetime());
				return ps;
			}
		},holder);
	}
	private void updatefile(Ureport2File file) {
		jdbcTemplate.update("update wb_ureports set prefix =? ,filename = ? ,filecontent = ? ,deleteflag = ? ,createtime = ? ,updatetime = ? where id = ? ",   
                new Object[]{file.getPrefix(),file.getFilename(),file.getFilecontent(),file.getDeleteflag(),file.getCreatetime(),file.getUpdatetime(),file.getId()});
	}
	@Override
	public void saveFile(Ureport2File file) {
		//Ureport2File oldfile = (Ureport2File) dao.findByPropertyFirst(Ureport2File.class, "prefix",file.getPrefix(),"filename",file.getFilename());
		List<Ureport2File> oldfiles = jdbcTemplate.query(" SELECT * FROM wb_ureports WHERE prefix=? AND filename=?", new Ureport2FileRowMapper(), new Object[] {file.getPrefix(),file.getFilename()});
		if(null==oldfiles || oldfiles.size()==0) {
			savefile(file);
		}else {
			Ureport2File oldfile = oldfiles.get(0);
			oldfile.setDeleteflag(false);
			oldfile.setFilecontent(file.getFilecontent());
			oldfile.setUpdatetime(new Date());
			updatefile(oldfile);
		}
	}
	@Override
	public List<Ureport2File> getFiles(String prefix) {
		List<Ureport2File> files = jdbcTemplate.query(" SELECT * FROM wb_ureports WHERE prefix=? AND deleteflag=? ", new Ureport2FileRowMapper(), new Object[] {prefix,false});
		return files;
	}
	@Override
	public Ureport2File getFile(String prefix, String filename) {
		Ureport2File oldfile = jdbcTemplate.queryForObject(" SELECT * FROM wb_ureports WHERE prefix=? AND filename=? AND deleteflag=?", new Ureport2FileRowMapper(), new Object[] {prefix,filename,false});
		return oldfile;
	}
	@Override
	public void deleteFile(String prefix, String filename) {
		Ureport2File oldfile = getFile(prefix,filename);
		oldfile.setDeleteflag(true);
		updatefile(oldfile);
	}
}
